{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b9a014e3",
   "metadata": {},
   "source": [
    "# 定义人脸识别的函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "23aed509",
   "metadata": {},
   "source": [
    "## 人脸检测的定义函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "2bc1c879",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "API_key = '4udXjI9wvOnmb6uKVqr7gS0iwvSQcmc6'\n",
    "API_sercret = 'FtFY8-LLw7rHqVd6NEsUviJ7PJE5ASJg'\n",
    "def face_detect(API_key,API_sercret,image_path,landmark=None,attributes=None,face_rectangle=None,beauty_score_min=0,beauty_score_max=100):\n",
    "    \"\"\"该函数为调用face++ face_detect接口,用于人脸检测\"\"\"\n",
    "    base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    files = {'image_file': (image_path, open(image_path, 'rb'), 'multipart/form-data')}\n",
    "    payload = {\n",
    "            'api_key':API_key,\n",
    "            'api_secret':API_sercret,\n",
    "            \"return_landmark\":landmark,\n",
    "            \"return_attributes\":attributes,\n",
    "            \"face_rectangle\":face_rectangle,\n",
    "            \"beauty_score_min\":beauty_score_min,\n",
    "            \"beauty_score_max\":beauty_score_max\n",
    "            \n",
    "            }\n",
    "    \n",
    "\n",
    "    r = requests.post(base_url, params = payload,files = files )\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0c3d23be",
   "metadata": {},
   "source": [
    "## 人脸分析的定义函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "79692ede",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "API_key = '4udXjI9wvOnmb6uKVqr7gS0iwvSQcmc6'\n",
    "API_sercret = 'FtFY8-LLw7rHqVd6NEsUviJ7PJE5ASJg'\n",
    "def face_analyze(API_Key,API_Secret,face_tokens,attributes):\n",
    "    \"\"\"该函数为调用face++ face_analyze接口，用于人脸分析\"\"\"\n",
    "    url = 'https://api-cn.faceplusplus.com/facepp/v3/face/analyze'\n",
    "    payload = {\n",
    "        \"api_key\":API_Key,\n",
    "        \"api_secret\":API_Secret,\n",
    "        \"face_tokens\":face_tokens,\n",
    "        \"return_attributes\":attributes\n",
    "    }\n",
    "    r = requests.post(url,params=payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1d4c865a",
   "metadata": {},
   "source": [
    "## 人脸对比的定义函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "fe47450b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "API_key = '4udXjI9wvOnmb6uKVqr7gS0iwvSQcmc6'\n",
    "API_sercret = 'FtFY8-LLw7rHqVd6NEsUviJ7PJE5ASJg'\n",
    "def face_compare(API_Key,API_secret,face_token1,face_token2):\n",
    "    \"\"\"该函数为调用face++ face_compare接口，用于人脸对比\"\"\"\n",
    "    url = 'https://api-cn.faceplusplus.com/facepp/v3/compare'\n",
    "    payload = {\n",
    "        \"api_key\":API_Key,\n",
    "        \"api_secret\":API_secret,\n",
    "        \"face_token1\":face_token1,\n",
    "        \"face_token2\":face_token2\n",
    "    }\n",
    "    r = requests.post(url,params=payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0f3195ce",
   "metadata": {},
   "source": [
    "## 人脸搜索的定义函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "084de436",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "API_key = '4udXjI9wvOnmb6uKVqr7gS0iwvSQcmc6'\n",
    "API_sercret = 'FtFY8-LLw7rHqVd6NEsUviJ7PJE5ASJg'\n",
    "def face_search(API_Key,API_Secret,face_token,faceset_token):\n",
    "    \"\"\"该函数为调用face++ face_search接口，用于人脸查找\"\"\"\n",
    "    url = \"https://api-cn.faceplusplus.com/facepp/v3/search\"\n",
    "    payload = {\n",
    "        \"api_key\":API_Key,\n",
    "        \"api_secret\":API_Secret,\n",
    "        \"face_token\":face_token,\n",
    "        \"faceset_token\":faceset_token\n",
    "    }\n",
    "    r = requests.post(url,params=payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "303ccaf2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function face_detect in module __main__:\n",
      "\n",
      "face_detect(API_key, API_sercret, image_path, landmark=None, attributes=None, face_rectangle=None, beauty_score_min=0, beauty_score_max=100)\n",
      "    该函数为调用face++ face_detect接口,用于人脸检测\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(face_detect)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "334e464b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function face_analyze in module __main__:\n",
      "\n",
      "face_analyze(API_Key, API_Secret, face_tokens, attributes)\n",
      "    该函数为调用face++ face_analyze接口，用于人脸分析\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(face_analyze)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "5d9a155e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function face_compare in module __main__:\n",
      "\n",
      "face_compare(API_Key, API_secret, face_token1, face_token2)\n",
      "    该函数为调用face++ face_compare接口，用于人脸对比\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(face_compare)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "eaf98031",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function face_search in module __main__:\n",
      "\n",
      "face_search(API_Key, API_Secret, face_token, faceset_token)\n",
      "    该函数为调用face++ face_search接口，用于人脸查找\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(face_search)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35f18e2b",
   "metadata": {},
   "source": [
    "## 宿舍门禁系统问题"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "285f1115",
   "metadata": {},
   "source": [
    "#### 第一步-先创建一个人脸集合\n",
    "#### 第二步-把成员人脸添加进去并获取他的token\n",
    "#### 再拿另一张照片过去比对\n",
    "#### 分析照片之间的相似度，来测试是否符合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "a0f8da4a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648548975,ead1744b-68da-4f91-9c5e-dbee8a6e4962',\n",
       " 'time_used': 109,\n",
       " 'faces': [{'face_token': 'b8371a69bf705bf084a78ac277af6c52',\n",
       "   'face_rectangle': {'top': 472, 'left': 96, 'width': 566, 'height': 566}}],\n",
       " 'image_id': 'LMafIjMyNFc9EoYp3Gu5VA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import requests\n",
    "API_key = '4udXjI9wvOnmb6uKVqr7gS0iwvSQcmc6'\n",
    "API_sercret = 'FtFY8-LLw7rHqVd6NEsUviJ7PJE5ASJg'\n",
    "face_detect(API_key,API_sercret,'1.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "8de56486",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648548977,55764ad8-8ae7-4955-a120-e02da263b687',\n",
       " 'time_used': 142,\n",
       " 'faces': [{'face_token': 'e4baf401b32ab1b972a6fa57eb6a0ba6',\n",
       "   'face_rectangle': {'top': 492, 'left': 280, 'width': 422, 'height': 422}}],\n",
       " 'image_id': 'lE9qOMXH8mw3mKN73FDUkA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import requests\n",
    "API_key = '4udXjI9wvOnmb6uKVqr7gS0iwvSQcmc6'\n",
    "API_sercret = 'FtFY8-LLw7rHqVd6NEsUviJ7PJE5ASJg'\n",
    "face_detect(API_key,API_sercret,'2.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "aae02323",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648548979,2dc5b54c-5b1b-47bb-8243-ff0e60510d8e',\n",
       " 'time_used': 99,\n",
       " 'faces': [{'face_token': 'a1154871bc924b1b3cd533d4daea239c',\n",
       "   'face_rectangle': {'top': 369, 'left': 162, 'width': 339, 'height': 339}}],\n",
       " 'image_id': 'JLijlSRkaZAnCPffNFx5cQ==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import requests\n",
    "API_key = '4udXjI9wvOnmb6uKVqr7gS0iwvSQcmc6'\n",
    "API_sercret = 'FtFY8-LLw7rHqVd6NEsUviJ7PJE5ASJg'\n",
    "face_detect(API_key,API_sercret,'3.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b8ff26ca",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "3ae08fc3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建人脸库\n",
    "import requests\n",
    "API_key = '4udXjI9wvOnmb6uKVqr7gS0iwvSQcmc6'\n",
    "API_sercret = 'FtFY8-LLw7rHqVd6NEsUviJ7PJE5ASJg'\n",
    "FaceSet_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "payload = {\n",
    "    \"api_key\":API_key,\n",
    "    \"api_secret\":API_sercret,\n",
    "    'outer_id':\"t_faces\",\n",
    "    'display_name':\"东23-115\",\n",
    "    'tags':\"schoolgirl\",\n",
    "    'face_tokens':\"b8371a69bf705bf084a78ac277af6c52,e4baf401b32ab1b972a6fa57eb6a0ba6,a1154871bc924b1b3cd533d4daea239c\",\n",
    "    'user_data':\"3人---广东\"\n",
    "}\n",
    "r = requests.post(url = FaceSet_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "1cf5df6f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "d4382566",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '9cd0d05587dda919f9fc3ef99dcfe9d7',\n",
       " 'time_used': 874,\n",
       " 'face_count': 3,\n",
       " 'face_added': 3,\n",
       " 'request_id': '1648549078,e2fd42ba-e8ef-4edc-802f-907de1ae593c',\n",
       " 'outer_id': 't_faces',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "14626282",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'face_token'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-59-d20b28666d8d>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mfaceset_token\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mr\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mjson\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'face_token'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[0mfaceset_token\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyError\u001b[0m: 'face_token'"
     ]
    }
   ],
   "source": [
    "faceset_token = r.json()['face_token']\n",
    "faceset_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "371c62d2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 查询人脸库的信息\n",
    "url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail'  \n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'face_tokens':\"b8371a69bf705bf084a78ac277af6c52,e4baf401b32ab1b972a6fa57eb6a0ba6,a1154871bc924b1b3cd533d4daea239c\",\n",
    "}\n",
    "r = requests.post(url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "ab7a0185",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [400]>"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "c32edb2b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 70,\n",
       " 'error_message': 'MISSING_ARGUMENTS: faceset_token, outer_id',\n",
       " 'request_id': '1648549081,006308ce-9fd5-44ff-8e4d-d170b4e0cf1a'}"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "088a94d5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 获取人脸库的信息\n",
    "url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'outer_id':'t_faces'\n",
    "}\n",
    "r = requests.post(url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "ab9a043c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "630bf1e6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<bound method Response.json of <Response [200]>>"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "b37c78d7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648549361,a84b2f9a-eff5-4db2-8140-0900358f055c',\n",
       " 'time_used': 141,\n",
       " 'faces': [{'face_token': 'e6c90a7327d21e0d6e7cfcc0e3141926',\n",
       "   'face_rectangle': {'top': 179, 'left': 32, 'width': 520, 'height': 520}}],\n",
       " 'image_id': 'dOkUADIpD+IWi8Tg7ndIUA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 另一张图片\n",
    "face_detect(API_key,API_sercret,'4.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "0fb940d6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648549556,016d5df5-88a5-4a0e-8f2c-869eea97f04c',\n",
       " 'time_used': 425,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-5': 73.975, '1e-4': 69.101},\n",
       " 'results': [{'confidence': 45.711,\n",
       "   'user_id': '',\n",
       "   'face_token': 'a1154871bc924b1b3cd533d4daea239c'}]}"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import requests\n",
    "API_key = '4udXjI9wvOnmb6uKVqr7gS0iwvSQcmc6'\n",
    "API_sercret = 'FtFY8-LLw7rHqVd6NEsUviJ7PJE5ASJg'\n",
    "url = \"https://api-cn.faceplusplus.com/facepp/v3/search\"\n",
    "payload = {\n",
    "        \"api_key\":API_key,\n",
    "        \"api_secret\":API_sercret,\n",
    "        \"face_token\":'e6c90a7327d21e0d6e7cfcc0e3141926',\n",
    "        'outer_id':\"t_faces\",\n",
    "    }\n",
    "r = requests.post(url,params=payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "0c00c2e2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648549725,782fc12a-02fc-43fa-b2cf-c6527863a80a',\n",
       " 'time_used': 352,\n",
       " 'confidence': 45.605,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "url = 'https://api-cn.faceplusplus.com/facepp/v3/compare'\n",
    "payload = {\n",
    "        \"api_key\":API_key,\n",
    "        \"api_secret\":API_sercret,\n",
    "        \"face_token1\":'e6c90a7327d21e0d6e7cfcc0e3141926',\n",
    "        \"face_token2\":'a1154871bc924b1b3cd533d4daea239c'\n",
    "    }\n",
    "r = requests.post(url,params=payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "a7f92122",
   "metadata": {},
   "outputs": [],
   "source": [
    "confidence = r.json()['confidence']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "514940e6",
   "metadata": {},
   "outputs": [],
   "source": [
    "thresholds= r.json()['thresholds']['1e-5']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "f4010201",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "不是我的舍友\n"
     ]
    }
   ],
   "source": [
    "if confidence > thresholds:\n",
    "    print('是我的舍友')\n",
    "else:\n",
    "    print('不是我的舍友')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "163df9d2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
